package com.cheer.ba.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.model.CityModel;
import com.cheer.ba.model.DistrictsModel;
import com.cheer.ba.model.MyAddressModel;
import com.cheer.ba.model.ProvinceModel;
import com.cheer.ba.presenter.impl.MyAddressPresenter;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.pickerview.OptionsPickerView;
import com.kyleduo.switchbutton.SwitchButton;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 描述 ：编辑常用地址
 * 作者 ：hq
 * 时间 ：2017/5/23 16:55
 */
public class FrequentlyAddressAty extends BaseCommAty {

    @BindView(R.id.id_person_name_txt)
    EditText mEditName;
    private String mSaveName;

    @BindView(R.id.id_person_phone)
    EditText mEditPhone;
    private String mSavePhone;

    @BindView(R.id.id_person_area)
    TextView mTxtArea;
    private String mSaveArea;

    @BindView(R.id.id_detail_address_edit)
    EditText mEditDetailAddress;
    private String mSaveAddress;

    // 是否设置为默认
    @BindView(R.id.id_default_rela)
    RelativeLayout mRelaDefault;
    //
    @BindView(R.id.id_address_switchbtn)
    SwitchButton mSwitchButton;
    private boolean isDefault;

    // 是否删除地址
    @BindView(R.id.id_del_address_txt)
    TextView mTxtDelAddress;


    private MyAddressModel model;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_frequently_address, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new MyAddressPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.frequently_use_address));
        setTitleRight(getString(R.string.sure_btn), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkData()) {
                    LogUtils.LogD("地址", mSaveArea + "|" + mSaveAddress);
                    ((MyAddressPresenter) presenter).addMyAddress(mSaveName, mSavePhone, mSaveArea + "|" + mSaveAddress);
                }
            }
        });
        model = (MyAddressModel) getIntent().getSerializableExtra(IntentKey.General.KEY_MODEL);
        if (null != model) {
            mTxtDelAddress.setVisibility(View.VISIBLE);
            if (model.getIsMain() == 1) {
                mRelaDefault.setVisibility(View.GONE);
            } else {
                mRelaDefault.setVisibility(View.GONE);
            }
            mEditName.setText(model.getName());
            mEditPhone.setText(model.getTel());
            mTxtArea.setText(model.getProvince());
            mEditDetailAddress.setText(model.getCity());
        } else {
            mTxtDelAddress.setVisibility(View.GONE);
            mRelaDefault.setVisibility(View.GONE);
        }
        mSwitchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isDefault = isChecked;
            }
        });
    }

    public boolean checkData() {
        mSaveName = mEditName.getText().toString().trim();
        mSavePhone = mEditPhone.getText().toString().trim();
        mSaveAddress = mEditDetailAddress.getText().toString().trim();
        if (StringUtils.isEmpty(mSaveName) || mSaveName.length() < 2) {
            SVProgressHUD.showInfoWithStatus(aty, getString(R.string.input_name));
        } else {
            if (StringUtils.isEmpty(mSavePhone) || mSavePhone.length() < 7) {
                SVProgressHUD.showInfoWithStatus(aty, getString(R.string.input_person_phone));
            } else {
                if (StringUtils.isEmpty(mSaveArea)) {
                    SVProgressHUD.showInfoWithStatus(aty, getString(R.string.input_area));
                } else {
                    if (StringUtils.isEmpty(mSaveAddress)) {
                        SVProgressHUD.showInfoWithStatus(aty, getString(R.string.input_detail_address));
                    } else {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @OnClick({R.id.id_choose_address_linear, R.id.id_del_address_txt})
    public void onClick(View mView) {
        switch (mView.getId()) {
            case R.id.id_choose_address_linear:
                //添加地址
                Intent email = new Intent(aty, ProvinceAreaAty.class);
                startActivityForResult(email, IntentCode.Area.PROVINCE_REQUEST_CODE);
                break;
            case R.id.id_del_address_txt:
                // 移除地址
                ((MyAddressPresenter) presenter).delMyAddress(model.getUid());
                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.DEL_SUCCESS)) {
                // 移除地址
                SVProgressHUD.showSuccessWithStatus(aty, "删除成功");
                BaseEvent event = new BaseEvent();
                event.type = ConstantTag.DEL_SUCCESS;
                EventBus.getDefault().post(event);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 2000);
            } else if (TextUtils.equals(tag, ConstantTag.ADD_SUCCESS)) {
                // 添加地址成功
                MyAddressModel addressModel = (MyAddressModel) t;
//                if (isDefault) {
//                    if (null != addressModel) {
//                        //设置默认地址
//                        ((MyAddressPresenter) presenter).defaultMyAddress(addressModel.getUid());
//                        return;
//                    }
//                }
                SVProgressHUD.showSuccessWithStatus(aty, "添加地址成功");
                BaseEvent event = new BaseEvent();
                event.type = ConstantTag.DEL_SUCCESS;
                EventBus.getDefault().post(event);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 2000);
            } else if (TextUtils.equals(tag, ConstantTag.DEFAULT_SUCCESS)) {
                //设置默认地址成功
                BaseEvent event = new BaseEvent();
                event.type = ConstantTag.DEL_SUCCESS;
                EventBus.getDefault().post(event);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 2000);
            } else if (TextUtils.equals(tag, ConstantTag.FAIL_ACTION)) {
                //设置默认地址失败
                BaseEvent event = new BaseEvent();
                event.type = ConstantTag.DEL_SUCCESS;
                EventBus.getDefault().post(event);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 2000);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentCode.Area.PROVINCE_REQUEST_CODE) {
            if (resultCode == IntentCode.Area.PROVINCE_RESULT_CODE) {
                if (null != data) {
                    mSaveArea = data.getStringExtra(IntentKey.General.KEY_DATA);
                    mTxtArea.setText(mSaveArea);
                    Session.setUser_area(mSaveArea);
                    String[] s = StringUtils.strSplit(mSaveArea);
                    mSaveArea = s[0] + "|" + s[1] + "|" + s[2];
                    LogUtils.LogD("地址4", mSaveArea);
                }
            }
        }
    }
}
